﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web.Configuration;
using System.Configuration;
using System.Data;
using System.Web.Security; 

/// <summary>
/// Summary description for BaseDao
/// </summary>
/// 

namespace DAO
{
    public class BaseDAO
    {
        private static string connectionString =null;

        protected BaseDAO()
        {

        }

    //    SqlConnection connection = new SqlConnection(connectionString);


        public static SqlDataReader ExecuteReader(SqlCommand command)
        {
            if (connectionString == null)
            {

                Configuration webConfiguration = WebConfigurationManager.OpenWebConfiguration("/Admin");
                ConnectionStringSettings connectionStringSettings =
                webConfiguration.ConnectionStrings.ConnectionStrings["HMSConnectionString"];
                connectionString = connectionStringSettings.ConnectionString;
            }

            SqlConnection connection = new SqlConnection(connectionString);
            command.Connection = connection;

            try
            {
                connection.Open();
                SqlDataReader reader;
                reader = command.ExecuteReader();
                return reader;
            }
            finally
            {
                connection.Close();
            }
        }
        private static DataTable executeHelper(SqlCommand command, bool isNonQuery)
        {
            if (connectionString == null)
            {
                Configuration webConfiguration = WebConfigurationManager.OpenWebConfiguration("/Admin");
             ConnectionStringSettings connectionStringSettings =
                webConfiguration.ConnectionStrings.ConnectionStrings["HMSConnectionString"];
               connectionString = connectionStringSettings.ConnectionString;
           
       }


            SqlConnection connection = new SqlConnection(connectionString);
            command.Connection = connection;

            try
            {
                connection.Open();

                if (isNonQuery)
                {
                    command.ExecuteNonQuery();
                    return null;
                }
                else
                {
                    DataTable dataTable = new DataTable();
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                    dataAdapter.Fill(dataTable);
                    return dataTable;
                }
            }
            finally
            {
                connection.Close();
            }
        }

        protected static void executeNonQuery(SqlCommand command)
        {
            executeHelper(command, true);
        }

        protected static DataTable executeQuery(SqlCommand command)
        {
            return executeHelper(command, false);
        }
    }
}